#!/usr/bin/env python
#encoding=utf-8

import re as regex
import json

from login import *
from util import *

#获取章节与资源列表
def get_chapters(url, chapter_list):
    page = get_webpage(url)
    ftp_regex = regex.compile(r'ftp://[^"\']+')
    chapter_match_list = ftp_regex.findall(page)
    if len(chapter_match_list) == 0:
        print('    没捕获到资源:' + url)
        return

    curr_filename = ''
    curr_resource = []
    for item in chapter_match_list:
        file_name = get_url_filename(item)
        if file_name != curr_filename:
            curr_filename = file_name
            curr_resource = []
            chapter_list.append({'filename':curr_filename, 'resource':curr_resource})
        curr_resource.append(item)
    pass

#讲义
def get_lecture(course_id, chapter_list):
    print('  获取课程讲义')
    url = 'http://elearning.med66.com/cware/download/wordDownload.shtm?wordType=1&cwareID=' + course_id
    get_chapters(url, chapter_list)

#习题
def get_practice(course_id, chapter_list):
    print('  获取课程习题')
    url = 'http://elearning.med66.com/cware/download/wordDownload.shtm?wordType=2&cwareID=' + course_id
    get_chapters(url, chapter_list)

#安卓视频
def get_phone_video(course_id, chapter_list):
    print('  获取课程视频')
    url = 'http://elearning.med66.com/cware/download/videoDownload.shtm?cwareDownType=down12&cwareID=' + course_id
    get_chapters(url, chapter_list)

def parse_resource_page(course_id, resource_info):
    #获取讲义列表
    lecture_list = [] 
    resource_info['lecture'] = lecture_list
    get_lecture(course_id, lecture_list)

    #获取习题资源列表
    practice_list = []
    resource_info['practice'] = practice_list
    get_practice(course_id, practice_list)

    #获取安卓资源列表
    phone_video_list = []
    resource_info['phone_video'] = phone_video_list
    get_phone_video(course_id, phone_video_list)
    pass

def parse_course_list_page(resource):
    url = 'http://member.med66.com/mycenter/index.shtm?'
    course_regex = regex.compile(r'(?m)^.*checkEnter\(\'(\d{6})\'\).*<span class="bc3">(.*?)</span>.*$')
    page = get_webpage(url)
    #write_file(page, 'course_list.html')  #DEBUG

    course_match_list = course_regex.findall(page)
    resource['is_done'] = False
    course_list = []
    resource['course_list'] = course_list
    for item in course_match_list:
        course_id = item[0]
        course_name = item[1].decode('gbk')
        print(course_id + ', ' + course_name)
        resource_info = {}
        course_info = {'course_name':course_name, 'course_id':course_id, 'resource':resource_info}
        course_list.append(course_info)
        parse_resource_page(course_id, resource_info)
    resource['is_done'] = True

def get_resource():
    resource = {}

    json_file = 'resource.json'
    #如果没有source_list.json
    if os.path.exists(json_file):
        #读出json文件，并解释
        print('导入json文件')
        f = file(json_file)
        resource = json.load(f)
        pass
    else:
        if login():
            #开始遍历
            try:
                parse_course_list_page(resource)
            except KeyboardInterrupt:
                pass
            #保存到source_list.json文件
            json_context = json.dumps(resource, indent=4)
            write_file(json_context, json_file)
        else:
            print('登陆失败')

    return resource

if __name__ == '__main__':
    rl = get_resource()
    print rl;
